<template>
  <!-- 对话框(添加 / 修改) -->
  <el-dialog
    title="添加排课"
    :visible.sync="show"
    width="35%"
    append-to-body
    :before-close="cancel"
    :close-on-click-modal="false"
  >
    <!-- <class-selector /> -->
    <el-form
      ref="form"
      :model="form"
      :rules="rules"
      :inline="true"
      label-width="80px"
    >
      <el-form-item label="学院" prop="collegeId">
        <college-selector v-model="form.collegeId"></college-selector>
      </el-form-item>

      <el-form-item label="选择课程" prop="subjectId">
        <el-select v-model="form.subjectId" filterable placeholder="请选择">
          <el-option
            v-for="item in courses"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="选择教师" prop="teacherId">
        <teacher-selector
          v-model="form.teacherId"
          :college-id="form.collegeId"
        ></teacher-selector>
      </el-form-item>
      <el-form-item label="选择星期" prop="weekId">
        <el-select v-model="form.weekId" filterable placeholder="请选择">
          <el-option
            v-for="item in weeks"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="课时选择" prop="studyTimeId">
        <el-select v-model="form.studyTimeId" filterable placeholder="请选择">
          <el-option
            v-for="item in classTime"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="教室选择" prop="roomId">
        <el-select v-model="form.roomId" filterable placeholder="请选择">
          <el-option
            v-for="item in classroom"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="教学模式" prop="classMode">
        <el-radio-group v-model="form.classMode">
          <el-radio :label="1">中文</el-radio>
          <el-radio :label="2">外语</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="课表类型" prop="type">
        <el-radio-group v-model="form.type">
          <el-radio :label="1">固定课表</el-radio>
          <el-radio :label="2">临时课表</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="备注" prop="remark">
        <el-input type="textarea" v-model="form.remark"></el-input>
      </el-form-item>
    </el-form>

    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submitForm">确 定</el-button>
      <el-button @click="cancel">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { timeAllList, updateClassTimetable, createClassTimetable } from "@/api/study/classTimetable";
import { getClassroomInfoPage } from "@/api/collegebase/classroomInfo";
import Editor from "@/components/Editor";
import common from "@/api/collegebase/common";
import ClassSelector from "@/components/ClassSelector";
import { getSubjectPage } from "@/api/study/subject";
import { getTeacherTeachersPage } from "@/api/collegebase/teacherTeachers";
import CollegeSelector from "@/components/CollegeSelector";
import TeacherSelector from "@/components/TeacherSelector";

export default {
  name: "ClassesEdit",
  components: {
    TeacherSelector,
    CollegeSelector,
    Editor,
    ClassSelector,
  },
  data() {
    return {
      model: "",
      week: "",
      time: "",
      room: "",
      weeks: [
        {
          value: 1,
          label: "星期一",
        },
        {
          value: 2,
          label: "星期二",
        },
        {
          value: 3,
          label: "星期三",
        },
        {
          value: 4,
          label: "星期四",
        },
        {
          value: 5,
          label: "星期五",
        },
        {
          value: 6,
          label: "星期六",
        },
        {
          value: 7,
          label: "星期日",
        },
      ],
      options: [],
      value: "",

      // 表单参数
      form: {
        classMode: 1,
        type: 1,
        classesId: "",
        departmentsId: "",
        professionId: "",
      },
      // 表单校验
      rules: {
        subjectId: [{ required: true, message: "请选择课程", trigger: "blur" }],
        teacherId: [{ required: true, message: "请选择教师", trigger: "blur" }],
        collegeId: [
          { required: true, message: "请选择学院", trigger: "blur" },
        ],
        weekId: [{ required: true, message: "请选择星期", trigger: "blur" }],
        studyTimeId: [
          { required: true, message: "请选择课时", trigger: "blur" },
        ],
        roomId: [{ required: true, message: "请选择教室", trigger: "blur" }],
        classMode: [
          { required: true, message: "请选择教学模式", trigger: "blur" },
        ],
        type: [{ required: true, message: "请选择课表类型", trigger: "blur" }],
      },
      classList: [],
      classTime: [],
      classroom: [],
      courses: [],
      teachers: [],
    };
  },
  created() {
    this.getData();
  },
  props: {
    // 是否显示弹出层
    show: {
      type: Boolean,
      default: false,
    },
    // 显示弹出层标题
    title: {
      type: String,
      default: "",
    },
  },
  methods: {
    //获取班级
    async professionChange(professionId) {
      this.$set(this.form, "classesId", "");
      const res = await common.getClassesList(professionId);
      this.classList = res.data;

      this.$forceUpdate();
    },


    //获取
    async getData() {
      timeAllList().then((res) => {
        res.data.forEach((item) => {
          item.label = item.beginTime + "-" + item.endTime;
          item.value = item.id;
        });
        this.classTime = res.data;
      });
      getClassroomInfoPage({ pageNo: 1, pageSize: 99 }).then((res) => {
        res.data.list.forEach((e) => {
          e.label = e.classroomName;
          e.value = e.id;
        });
        this.classroom = res.data.list;
      });
      getSubjectPage({ pageNo: 1, pageSize: 99 }).then((res) => {
        res.data.list.forEach((e) => {
          e.label = e.name;
          e.value = e.id;
        });
        this.courses = res.data.list;
      });
      getTeacherTeachersPage({ pageNo: 1, pageSize: 99 }).then((res) => {
        res.data.list.forEach((e) => {
          e.label = e.teacherName;
          e.value = e.id;
        });
        this.teachers = res.data.list;
      });
    },
    /** 初始化编辑的数据**/
    initData(rowData) {
      this.reset();

      rowData = JSON.parse(rowData);
      console.log(rowData);

      if (rowData) {
        this.form = rowData;
        console.log(this.form);
        this.form.datetimerange = [this.form.startTime, this.form.endTime];
      }
    },
    /** 取消按钮 */
    cancel() {
      this.$emit("update:show", false);
      this.reset();
    },
    /** 表单重置 */
    reset() {
      this.form = {
        id: undefined,
        classesNumber: undefined,
        classesName: undefined,
        classesCode: undefined,
        professionId: undefined,
        classesGrade: undefined,
        openingTime: undefined,
        orgId: undefined,
        remark: undefined
      };
      console.log(this.form);
      this.resetForm("form");
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (!valid) {
          return;
        }
        // 修改的提交
        if (this.form.id != null) {
          updateClassTimetable(this.form).then((response) => {
            this.$modal.msgSuccess("修改成功");
            this.cancel();
            this.$emit("success");
          });
          return;
        }
        // 添加的提交
        createClassTimetable(this.form).then((response) => {
          this.$modal.msgSuccess("新增成功");
          this.cancel();
          this.$emit("success");
        });
      });
    },
  },
};
</script>
